# coding: utf-8
import numpy as np
import pandas as pd
import os
import sys
from sklearn.pipeline import Pipeline
from sklearn.svm import LinearSVC
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import LabelBinarizer
from sklearn.metrics import accuracy_score
import warnings
warnings.filterwarnings("ignore")

if not os.path.exists("../text2vec/word2vec.txt"):
    print("../text2vec/word2vec.txt not exist")
    sys.exit()

if not os.path.exists("../text2vec/doc2vecs.txt"):
    print("../text2vec/doc2vecs.txt not exist")
    sys.exit()

with open("../text2vec/word2vec.txt", "r") as fr:
    word2vecs = [line.strip().split('\t') for line in fr]
for i, line in enumerate(word2vecs):
    word2vecs[i][1] = list(map(float, line[1].split()))
train = word2vecs[:int(len(word2vecs)*0.9)]
test = word2vecs[int(len(word2vecs)*0.9):]

x_train, y_train = [t[1] for t in train], [t[0] for t in train]
x_test, y_test = [t[1] for t in test], [t[0] for t in test]
lb = LabelBinarizer()
Y = lb.fit_transform(y_train)

def eval_score(preds, labels):
    temp = labels == preds
    temp = temp.astype(int)
    return sum(temp) / len(temp)

def get_eval(name, classifier):
    predicted = lb.inverse_transform(classifier.predict(x_test))
    print("%s: eval score on test dataset: %.3f" % (name, eval_score(predicted, y_test)))
    predicted = lb.inverse_transform(classifier.predict(x_train))
    print("%s: eval score on train dataset: %.3f" % (name, eval_score(predicted, y_train)))


# ## SVM
clf = OneVsRestClassifier(LinearSVC(class_weight='balanced'))

clf.fit(x_train, Y)
get_eval('[SVM on word2vec]', clf)


with open("../text2vec/doc2vecs.txt", "r") as fr:
    doc2vecs = [line.strip().split('\t') for line in fr]
for i, line in enumerate(doc2vecs):
    doc2vecs[i][1] = list(map(float, line[1].split()))
train = doc2vecs[:int(len(doc2vecs)*0.9)]
test = doc2vecs[int(len(doc2vecs)*0.9):]

x_train, y_train = [t[1] for t in train], [t[0] for t in train]
x_test, y_test = [t[1] for t in test], [t[0] for t in test]
lb = LabelBinarizer()
Y = lb.fit_transform(y_train)

# ## SVM
clf = OneVsRestClassifier(LinearSVC(class_weight='balanced'))

clf.fit(x_train, Y)
get_eval('[SVM on doc2vec]', clf)

